<?php
namespace Weixin\Controller;

use Think\Controller;
use Think\Page;

class WxKeywordController extends Controller {
	public function wxNewsIndex() {
		$keywords_reply = M ( 'wx_keywords_reply' );
		$count = M ( 'wx_keywords_reply' )->count ();
		
		$page = new Page ( $count, 6 );
		$limit = $page->firstRow . ',' . $page->listRows;
		
		$list = $keywords_reply->limit ( $limit )->select ();
		$this->assign ( 'all_keyword', $list );
		$this->assign ( 'page', $page->show () );
		$this->display ( 'wx_news_index' );
	}
	public function addKeyword() {
		if ($this->isPost ()) {
			$news_key = $_POST ['news_key'];
			$reply_detail = $_POST ['reply_detail'];
			$reply_type = $_POST ['reply_type'];
			$data = array (
					'keywords_text' => trim ( $this->replace_str ( $news_key ) ),
					'reply_detail' => trim ( $this->replace_str ( $reply_detail ) ),
					'reply_type' => $reply_type 
			);
			
			$m = M ( 'wx_keywords_reply' );
			
			$addFlag = $m->add ( $data );
			
			if ($addFlag) {
				$this->success ( '添加关键字回复成功！' );
			} else {
				$this->error ( '添加关键字回复失败！' );
			}
		}
	}
	public function ajaxUpdateKeyword() {
		$id = $_POST ['id'];
		$keywords_text = trim ( $this->replace_str ( $_POST ['keywords_text'] ) );
		$reply_detail = trim ( $this->replace_str ( $_POST ['reply_detail'] ) );
		
		$data = array (
				'keywords_text' => $keywords_text,
				'reply_detail' => $reply_detail 
		);
		$m = M ( 'wx_keywords_reply' );
		$updateFlag = $m->where ( "id=" . $id )->save ( $data );
		if ($updateFlag) {
			echo "1";
		} else {
			echo "0";
		}
		exit ();
	}
	function replace_str($str) {
		str_replace ( '\'', '', $str );
		str_replace ( "'", '', $str );
		str_replace ( '\"', '\'', $str );
		return $str;
	}
	public function ajaxDeleteKeyword() {
		$id = $_GET ['id'];
		
		$where = array (
				'id' => $id 
		);
		$deleteFlag = M ( 'wx_keywords_reply' )->where ( $where )->delete ();
		
		if ($deleteFlag) {
			echo "1";
		} else {
			echo "0";
		}
		exit ();
	}
}